<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/26
 * Time: 19:19
 */

namespace app\index\controller;

use app\index\model\Anchor as Attests;
use think\Db;
use think\Request;

class Anchor extends Base
{
    private $Anchor;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->Anchor = new Attests();
    }

    public function index()
    {
        $list = $this->Anchor->paginate();
        $this->assign('data', $list);
        return $this->fetch();
    }

    public function verify()
    {
        $id = input('get.id/d');
        $list = $this->Anchor->get($id);
        $this->assign('data', $list);
        return $this->fetch();
    }
    //审核
    public function check()
    {
        $data = input('post.');
        if(!$list = $this->Anchor->get($data['id'])){
            $r = resposer('审核记录不存在',0);
        }elseif($list['status'] !== '审核中'){
            $r = resposer('已经审核过了',0);
        }else{
            Db::startTrans();
            try{
                $this->Anchor->update($data);
                $room = new \app\index\model\Room();
                $roomData = ['uid'=>$list['uid'],'title'=>date('Ymd').$list['name']];
                $room->where(['uid'=>$list['uid']])->find()?$room->update($roomData,['uid'=>$list['uid']]):$room->insert($roomData);
                $r = resposer('审核成功', 1);
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                $r = resposer('审核失败', 0);
            }
        }
        return $r;
    }
}